package com.hyt.it.ogt.kq.service.gov.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.hyt.core.base.BaseServiceImpl;
import com.hyt.it.ogt.kq.service.gov.mapper.CustomArrangeMapper;
import com.hyt.it.ogt.kq.service.gov.model.dto.arrange.CustomArrangeDTO;
import com.hyt.it.ogt.kq.service.gov.model.entity.CustomArrange;
import com.hyt.it.ogt.kq.service.gov.service.ICustomArrangeService;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Optional;

/**
 * <p>
 * 自定义编排表 服务实现类
 * </p>
 *
 * @author huangyh
 * @since 2022-05-11
 */
@Service
public class CustomArrangeServiceImpl extends BaseServiceImpl<CustomArrangeMapper, CustomArrange> implements ICustomArrangeService {

    @Override
    public void deleteCandidateAdmissionNumbers(String taskId, List<String> candidateIdList) {
        QueryWrapper<CustomArrange> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda()
                .eq(CustomArrange::getTaskId, taskId)
                .in(CollectionUtils.isNotEmpty(candidateIdList), CustomArrange::getCandidateId, candidateIdList)
                .eq(CustomArrange::getDelFlag, false);
        this.remove(queryWrapper);
    }

    @Override
    public List<CustomArrange> listByTaskId(String taskId) {
        return this.lambdaQuery()
                .eq(CustomArrange::getTaskId, taskId)
                .eq(CustomArrange::getDelFlag, false)
                .list();
    }

    @Override
    public Optional<CustomArrange> getByCandidateId(String taskId, String candidateId) {
        return this.lambdaQuery()
                .eq(CustomArrange::getTaskId, taskId)
                .eq(CustomArrange::getCandidateId, candidateId)
                .last("LIMIT 1")
                .oneOpt();
    }

    @Override
    public String oneAdmissionNumberByCandidateId(String candidateId) {
        return this.lambdaQuery()
                .select(CustomArrange::getAdmissionNumber)
                .eq(CustomArrange::getCandidateId, candidateId)
                .eq(CustomArrange::getDelFlag, false)
                .last("LIMIT 1")
                .oneOpt()
                .map(CustomArrange::getAdmissionNumber)
                .orElse(null);
    }

    @Override
    public Optional<CustomArrange> oneAdmissionNumberByTaskId(String taskId) {
        return this.lambdaQuery()
                .eq(CustomArrange::getTaskId, taskId)
                .eq(CustomArrange::getDelFlag, false)
                .isNotNull(CustomArrange::getAdmissionNumber)
                .last("LIMIT 1")
                .oneOpt();
    }

    @Override
    public List<CustomArrange> listByCandidateId(String candidateId) {
        return this.lambdaQuery()
                .eq(CustomArrange::getCandidateId, candidateId)
                .eq(CustomArrange::getDelFlag, Boolean.FALSE)
                .list();
    }

    @Override
    public List<CustomArrangeDTO> listExistedByTaskId(String taskId) {
        return baseMapper.listExistedByTaskId(taskId);
    }

}
